package org.romzzz.gwtclassifieds.client.ui;

import org.romzzz.gwtclassifieds.client.i18n.LocalizedStrings;
import org.romzzz.gwtclassifieds.client.service.ClassifiedService;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * A popup dialog aiming at providing a simple way of sending feedback.
 */
public class FeedbackPrompt extends Dialog {
	
	protected FormPanel form;
	protected TextField<String> name;
	protected TextArea message;
	
	public FeedbackPrompt() {
		super();
		LocalizedStrings localizedStrings = GWT.create(LocalizedStrings.class);
		setLayout(new FitLayout());
		setHeading(localizedStrings.msgDeveloper());
		setDraggable(false);
		setResizable(false);
		form = new FormPanel();
		form.setHeaderVisible(false);
		form.setLabelWidth(60);
		form.setFieldWidth(240);
		name = new TextField<String>();
		name.setFieldLabel(localizedStrings.name());
		name.setAllowBlank(false);
		message = new TextArea();
		message.setFieldLabel(localizedStrings.message());
		message.setHeight(160);
		message.setAllowBlank(false);
		form.add(name);
		form.add(message);
		setHideOnButtonClick(false);
		setModal(true);
		setButtons(Dialog.OK);
		setSize(355, 275);
		Button ok = getButtonById(Dialog.OK);
		ok.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				sendMessage();
			}
		});
		name.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				if (event.getKeyCode() == 13) { 
					sendMessage();
				}
			}
		});
		message.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				if (event.getKeyCode() == 13) { 
					sendMessage();
				}
			}
		});
		setFocusWidget(name);
		FormButtonBinding binding = new FormButtonBinding(form);
		binding.addButton(ok);
		add(form);
	}
	
	private void sendMessage() {
		if (form.isValid()) {
			hide();
			final LocalizedStrings localizedStrings = GWT.create(LocalizedStrings.class);
			final MessageBox box = MessageBox.wait(localizedStrings.pleaseWait(), null, localizedStrings.sendingMessage());
			ClassifiedService.App.getInstance().sendFeedback(name.getValue(), message.getValue(), new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					box.close();
					MessageBox.alert(localizedStrings.error(), caught.toString(), null);
				}
				public void onSuccess(Boolean result) {
					box.close();
				}
			});
		}
	}

}
